#include <stdio.h>
#include <time.h>
#include <sys/time.h>

unsigned long long rpcc(void) {
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return (unsigned long long)ts.tv_sec * 1000000000ull + ts.tv_nsec;
  }

extern int Model_Predict(double *input);

int Matrix_Shape(int m, int k, int n, double *input) {
    // 计算最小和最大维度
    int min_dim = m;
    if (k < min_dim) min_dim = k;
    if (n < min_dim) min_dim = n;

    int max_dim = m;
    if (k > max_dim) max_dim = k;
    if (n > max_dim) max_dim = n;

    // // 判断是否为大写（>=2*min_dim）
    // int is_m_large = m >= 2 * min_dim;
    // int is_k_large = k >= 2 * min_dim;
    // int is_n_large = n >= 2 * min_dim;

    // 判断是否为小写（<=80且不满足大写）
    int is_m_small = (m <= 80);
    int is_k_small = (k <= 80);
    int is_n_small = (n <= 80);

    // 使用位掩码组合大写条件
    int mask = (is_m_small << 2) | (is_k_small << 1) | is_n_small;

    // 根据掩码快速判断形状类型
    switch (mask) {
        case 0b100: // m K N
            if (max_dim <= 2 * k && max_dim <= 2 * n)
            printf("MkN\n");
            else
            printf("Undefined Matrix Shape!\n");
            break;
        case 0b010: // MkN
            if (max_dim <= 2 * m && max_dim <= 2 * n)
            return Model_Predict(input);
            else
            printf("Undefined Matrix Shape!\n");
            break;
        case 0b001: // 
            if (max_dim <= 2 * m && max_dim <= 2 * k)
            printf("MKn\n");
            else
            printf("Undefined Matrix Shape!\n");
            break;
        case 0b110: // 
            printf("mkN\n");
            break;
        case 0b101: // 
            printf("mKn\n");
            break;
        case 0b011: // m K N
            printf("Mkn\n");
            break;
        case 0b000: // M K N
            //printf("MKN!\n");
            break;
        case 0b111: // M K N
            printf("mkn!\n");
            break;
        default:    // 其他未定义组合
            printf("Undefined Matrix Shape!\n");
            break;
    }
}


int Model_Judge(int m, int k, int n) {

    const int uTLB = (m * k + k * n + m * n) >> 10;
    if (uTLB <= 17) {
        return 1;
    }

    // 条件1改：至少一个是512的倍数则返回0
    if ((m & 0x1FF) == 0 || (k & 0x1FF) == 0 || (n & 0x1FF) == 0) {
        return 0;
    }

    double input[5] = {(double)uTLB/384, (double)uTLB/17, (double)m, (double)k, (double)n};

    return Matrix_Shape(m, k, n, input);
}

// int main(){

// unsigned long long Time_Model=0;
// double Time_Total=0.0;
// unsigned long long begin=0, end=0;

// int m = 288, k=72, n=144;

// begin = rpcc();
// int label = Model_Judge(m,k,n);
// end = rpcc();

// Time_Model = end - begin;
// printf("Time_Model: %10.9e\n", (double)Time_Model);
// printf("lable:%d\n", label);
// return 0;
// }